var Secretary = {
	sound:1,
	loaded:null,
	init:function(){
		// record the birth of secretary
		var date = new Date();
		Secretary.loaded = date;
		Secretary.evaluateEvents();
		$(document).unbind('clock.change').bind('clock.change',function(){
			Secretary.evaluateEvents();
			// evaluate how long it's been since refresh
			var now = new Date();
			var diff = DateTime.diff(Secretary.loaded,now,'h');
			// reload if over 3 hrs using same secretary
			if(diff > 3){
				Secretary.reload();
				console.warn('reloaded secretary. + now');
			}
		});
		// reload secretary when this gets triggered
		$(document).unbind('secretary.reload').bind('secretary.reload',function(){
			// but dont get too trigger happy
			Main.throttle(Secretary.reload,'secretaryReload',5000);
		});
	},
	evaluateEvents:function(){
		// hide all events first
		var $trs = $('#module-left-today-table tr[datetime]');
		var now = new Date();
		// modify against timezone
		var offset = now.getTimezoneOffset();
		
		// remove all showing classes
		$trs.hide().removeClass('show now soon later');
		// remove all labels
		$('tr.secretary-label-row').remove();
		
		// first show the next 8 hrs of stuffs
		$trs.each(function(){
			Secretary.evaluateEventByHour($(this),now);
		});
		$trs = $('#module-left-today-table tr[datetime].show');
		
		// if less than 3, then start looking ahead to tomrrow
		if($trs.length < 3){
			var count = 3 - $trs.length;
			$trs = $('#module-left-today-table tr[datetime]').not('.show');
			$trs.each(function(){
				if(count > 0){
					var found = Secretary.evaluateEventByNext($(this),now);
					if(found){count--;}
				}
			});
		}
		$('#module-left-today-table tr[datetime].show').show();
		
		// add labels to each category (if exists)
		// recent
		var $row = $('<tr class="secretary-label-row just"><td colspan="30">Recent</td></tr>');
		$('#module-left-today-table tr[datetime].just:first').before($row.clone());
		// now
		$row = $('<tr class="secretary-label-row now"><td colspan="30">Now<i class="fa fa-volume-up hidden"></i></td></tr>');
		$('#module-left-today-table tr[datetime].now:first').before($row.clone());
		// soon
		$row = $('<tr class="secretary-label-row soon"><td colspan="30">Soon</td></tr>');
		$('#module-left-today-table tr[datetime].soon:first').before($row.clone());
		// later
		$row = $('<tr class="secretary-label-row later"><td colspan="30">Later</td></tr>');
		$('#module-left-today-table tr[datetime].later:first').before($row.clone());
		
		// play sound if there's a new NOW item
		var isNew = false;
		var then = Cookie.get('secretaryThen');
		// loop because there may be more than 1 now
		var now = "";
		var id = "";
		var $now = $('tr[datetime].now').each(function(){
			id = $(this).attr('id');
			now += id;
			if(then.indexOf(id) == -1){
				isNew = true;
			}
		});

		Cookie.set('secretaryThen',now,2);
		if(isNew){
			// play sound
			if(Secretary.sound == 1){
				$('#secretaryNotification1').get(0).play();
			}
			if(Secretary.speech == 1){
				// get the text for sound
				var time = $('tr#'+id+' .today-start').text() || "";
				var text = $('tr#'+id+' .today-description').text() || "New Secretary Event";
				text = time + ', ' + text;
				text = encodeURIComponent(text);
				try{
					var audio = new Audio();
					audio.src = 'http://translate.google.com/translate_tts?ie=utf8&tl=en&q='+text+'&total=1&idx=0&client=t&prev=input';
					//audio.src = 'https://www.googleapis.com/language/translate/v2??ie=utf8&tl=en&q='+text+'&key=AIzaSyBKKwcm-gdDwZPRi2Qwtuth3ag06XmvfnI';
					audio.rel = 'noreferrer';
					audio.play();
				}catch(e){/**/}
			}
			Secretary.blinkTitle();
			// show icon
			$('.secretary-label-row.now i.fa-volume-up').removeClass('hidden');
			setTimeout(function(){
				$('.secretary-label-row.now i.fa-volume-up').addClass('hidden');	
			},20000);
		}
	},
	blinkTitle:function(){
		var oldTabText = document.title;
		var timer = 0;
		for(var i = 0; i < 20; i++){
			setTimeout(function(){
				if(document.title != '* New Secretary Task *'){
					document.title = '* New Secretary Task *';
				} else {
					document.title = $('tr[datetime].now:first .today-description').text() || "New Event";;
				}
			},timer);
			timer += 700;
		}
		// revert the title
		setTimeout(function(){
			document.title = oldTabText;
		},15000);
	},
	evaluateEventByHour:function($this,now){
		var start = new Date($this.attr('datetime'));
		var end = new Date($this.attr('datetimeend'));
		var startDiff = DateTime.diff(now,start,'m');
		var endDiff = DateTime.diff(now,end,'m');
		var show = false;
		var now = false;
		var soon = false;
		var just = false;
		var later = false;
		// evaluate just
		if(startDiff < 0 && endDiff < 0 && endDiff >= -30){
			just = true;
			$this.addClass('just');
		}
		// evaluate NOW
		if(startDiff <= 0 && endDiff >= 0){
			now = true;
			$this.addClass('now');
		}
		// evaluate soon
		if(startDiff > 0 && startDiff <= 120){
			soon = true;
			$this.addClass('soon');
		}
		// evaluate later
		if(startDiff > 120 && startDiff < 480){
			later = true;
			$this.addClass('later');
		}
		if(now || soon || later || just){
			$this.addClass('show');
		}
	},
	evaluateEventByNext:function($this,now){
		var start = new Date($this.attr('datetime'));
		var end = new Date($this.attr('datetimeend'));
		var startDiff = DateTime.diff(now,start,'m');
		var endDiff = DateTime.diff(now,end,'m');
		// evaluate now
		if(startDiff >= 480){
			$this.addClass('later show');
			return true;
		} else{
			return false;
		}
	},
	reload:function(){
		Ajax.run({
			method:"leftView.renderSecretary",
			onsuccess:function(data){
				$('#module-left-time-div').replaceWith(data);
				Secretary.init();
				Clock.init();
			}
		});
	},
	showOptions:function(){
		$('#secretary-show').fadeOut(function(){
			$('#secretary-edit').fadeIn();
		});
	},
	hideOptions:function(){
		$('#secretary-edit').fadeOut(function(){
			$('#secretary-show').fadeIn();
		});
	}
};